package com.spreedairy.util;

import java.util.List;


import javax.portlet.PortletURL;

import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.util.UnicodeFormatter;
import com.liferay.portlet.shopping.model.ShoppingCategory;
import com.liferay.portlet.shopping.service.ShoppingCategoryLocalServiceUtil;

public class CategoryUtil {

	public static String getSubCategoryPopupList(long groupId, long categoryId, String nameSpace) 
	throws SystemException{
		StringBuffer sb = new StringBuffer();
		
		sb.append("<div style=\"display:none;\" id=\"rowId"+categoryId+"\">");
		getPopupList(groupId, categoryId, nameSpace, sb);
		sb.append("</div>");
		
		return sb.toString();
	}
	
	public static void  getPopupList(long groupId, long categoryId, String nameSpace, StringBuffer sb) 
	throws SystemException{
		int count = ShoppingCategoryLocalServiceUtil.getCategoriesCount(groupId, categoryId);
		if(count > 0) {
			List list = ShoppingCategoryLocalServiceUtil.getCategories(groupId, categoryId, 0, count);
			sb.append("<ul><li>");
			for (int i=0; i < list.size(); i++) {
				ShoppingCategory category = (ShoppingCategory)list.get(i);
				category = category.toEscapedModel();
				//detailURL.setParameter("categoryId", String.valueOf(category.getCategoryId()));
				
				sb.append("<a href=\"javascript:void(0)\" onclick=\"");
				sb.append("opener.");
				sb.append(nameSpace);
				sb.append("selectCategory('");
				sb.append(category.getCategoryId());
				sb.append("', '");
				sb.append(UnicodeFormatter.toString(category.getName()));
				sb.append("'); window.close();");
				sb.append("\" >");
				sb.append(category.getName()+"</a><br>");
				if(category.getParentCategoryId() != 0) {
					getPopupList(groupId, category.getCategoryId(), nameSpace, sb);
				}
			}
			sb.append("</li></ul>");
		}
	}
	
	public static String getSubCategoryList(long groupId, long categoryId, PortletURL detailURL) 
	throws SystemException {
		StringBuffer sb = new StringBuffer();
		
			
		sb.append("<div style=\"display:none;\" id=\"rowId"+categoryId+"\">");
		getList(groupId, categoryId, detailURL, sb);
		sb.append("</div>");
		return sb.toString();
	}
	
	public static void  getList(long groupId, long categoryId, PortletURL detailURL, StringBuffer sb) 
	throws SystemException{
		int count = ShoppingCategoryLocalServiceUtil.getCategoriesCount(groupId, categoryId);
		if(count > 0) {
			List list = ShoppingCategoryLocalServiceUtil.getCategories(groupId, categoryId, 0, count);
			sb.append("<ul><li>");
			for (int i=0; i < list.size(); i++) {
				ShoppingCategory category = (ShoppingCategory)list.get(i);
				category = category.toEscapedModel();
				detailURL.setParameter("categoryId", String.valueOf(category.getCategoryId()));
				sb.append("<a href=\""+detailURL.toString()+"\">");
				sb.append("&raquo; "+category.getName()+"</a><br>");
				if(category.getParentCategoryId() != 0) {
					getList(groupId, category.getCategoryId(), detailURL, sb);
				}
			}
			sb.append("</li></ul>");
		}
	}
}
